package com.haixiaoke.saas.contract.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.auth.ali.service.AliAuthService;
import com.haixiaoke.saas.common.config.sms.SmsConfig;
import com.haixiaoke.saas.common.constant.ContractConstants;
import com.haixiaoke.saas.common.constant.UserConstants;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.utils.sms.SmsUtils;
import com.haixiaoke.saas.contract.domain.HouseCommission;
import com.haixiaoke.saas.contract.domain.Proprietor;
import com.haixiaoke.saas.contract.domain.ProxyService;
import com.haixiaoke.saas.contract.fadada.service.FaDaDaService;
import com.haixiaoke.saas.contract.mapper.ProprietorMapper;
import com.haixiaoke.saas.contract.service.IHouseCommissionService;
import com.haixiaoke.saas.contract.service.IProprietorService;
import com.haixiaoke.saas.contract.service.IProxyServiceService;
import com.haixiaoke.saas.department.service.IElectronicVisaService;
import com.haixiaoke.saas.order.domain.OrderLessee;
import com.haixiaoke.saas.user.domain.UserConsumer;
import com.haixiaoke.saas.user.service.IUserConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业主信息Service业务层处理
 *
 * @author xiaolong
 * @date 2024-06-06
 */
@Service
public class ProprietorServiceImpl implements IProprietorService {
    @Autowired
    private ProprietorMapper proprietorMapper;
    @Autowired
    private AliAuthService aliAuthService;
    @Autowired
    private IUserConsumerService userConsumerService;
    @Autowired
    private IHouseCommissionService houseCommissionService;
    @Autowired
    private IProxyServiceService proxyServiceService;

    /**
     * 查询业主信息
     *
     * @param proprietorId 业主信息主键
     * @return 业主信息
     */
    @Override
    public Proprietor selectProprietorByProprietorId(Long proprietorId) {
        Proprietor proprietor = proprietorMapper.selectProprietorByProprietorId(proprietorId);
        HouseCommission houseCommission = houseCommissionService.selectHouseCommissionByProprietorId(proprietorId);
        ProxyService proxyService = proxyServiceService.selectProxyServiceByProprietorId(proprietorId);
        proprietor.setHouseCommission(houseCommission);
        proprietor.setProxyService(proxyService);
        return proprietor;
    }

    /**
     * 查询业主信息列表
     *
     * @param proprietor 业主信息
     * @return 业主信息
     */
    @Override
    public List<Proprietor> selectProprietorList(Proprietor proprietor) {
        return proprietorMapper.selectProprietorList(proprietor);
    }

    /**
     * 新增业主信息
     *
     * @param proprietor 业主信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProprietor(Proprietor proprietor) {
        if (ContractConstants.PROPRIETOR_TYPE_PERSON.equals(proprietor.getProprietorType())) {
            if (StrUtil.isEmpty(proprietor.getCardNumber())) {
                throw new ServiceException("身份证号不能为空");
            }
            Boolean flag = aliAuthService.identityCheck(proprietor.getProprietorName(), proprietor.getCardNumber());
            if (!flag) {
                throw new ServiceException("名称和身份证不匹配");
            }
        } else {
            if (StrUtil.isEmpty(proprietor.getSocialCreditCode())) {
                throw new ServiceException("统一社会信用代码不能为空");
            }
            if (proprietor.getSocialCreditCode().length() != 18) {
                throw new ServiceException("统一社会信用证格式异常");
            }
        }

        List<Proprietor> proprietor1 = proprietorMapper.selectProprietorByProprietorName(proprietor.getProprietorName());
        if (CollectionUtil.isNotEmpty(proprietor1)) {
            throw new ServiceException("该名称已被注册为业主");
        }

        UserConsumer userConsumer = userConsumerService.selectUserConsumerByMobile(proprietor.getProprietorMobile());
        if (ObjectUtil.isEmpty(userConsumer)) {
            UserConsumer newConsumer = new UserConsumer();
            if (StrUtil.isNotBlank(proprietor.getCardNumber())) {
                int genderByIdCard = IdcardUtil.getGenderByIdCard(proprietor.getCardNumber());
                newConsumer.setSex(genderByIdCard == 1 ? OrderLessee.LESSEE_SEX_MAN : OrderLessee.LESSEE_SEX_WOMAN);
            }
            newConsumer.setUserMobile(proprietor.getProprietorMobile());
            newConsumer.setCreateTime(DateUtils.getNowDate());
            newConsumer.setNickName(proprietor.getProprietorName());
            newConsumer.setProprietorFlag(UserConstants.PROPRIETOR_FLAG_YES);
            newConsumer.setCreateBy(proprietor.getCreateBy());
            userConsumerService.insertUserConsumer(newConsumer);
            userConsumer = newConsumer;
        } else {
            Proprietor sqlProprietor = proprietorMapper.selectProprietorByUserId(userConsumer.getUserId());
            if (ObjectUtil.isNotEmpty(sqlProprietor)) {
                throw new ServiceException("该手机号已被注册为业主");
            }
            userConsumer.setProprietorFlag(UserConstants.PROPRIETOR_FLAG_YES);
            userConsumer.setNickName(proprietor.getProprietorName());
            userConsumer.setUpdateTime(DateUtils.getNowDate());
            userConsumer.setUpdateBy(proprietor.getCreateBy());
            userConsumerService.updateUserConsumer(userConsumer);
        }

        proprietor.setConUserId(userConsumer.getUserId());
        proprietor.setCreateTime(DateUtils.getNowDate());
        return proprietorMapper.insertProprietor(proprietor);
    }

    /**
     * 修改业主信息
     *
     * @param proprietor 业主信息
     * @return 结果
     */
    @Override
    public int updateProprietor(Proprietor proprietor) {
        proprietor.setUpdateTime(DateUtils.getNowDate());
        return proprietorMapper.updateProprietor(proprietor);
    }

    /**
     * 批量删除业主信息
     *
     * @param proprietorIds 需要删除的业主信息主键
     * @return 结果
     */
    @Override
    public int deleteProprietorByProprietorIds(Long[] proprietorIds) {
        return proprietorMapper.deleteProprietorByProprietorIds(proprietorIds);
    }

    /**
     * 删除业主信息信息
     *
     * @param proprietorId 业主信息主键
     * @return 结果
     */
    @Override
    public int deleteProprietorByProprietorId(Long proprietorId) {
        return proprietorMapper.deleteProprietorByProprietorId(proprietorId);
    }

    @Override
    public Proprietor selectProprietorByUserId(Long userId) {
        Proprietor proprietor = proprietorMapper.selectProprietorByUserId(userId);
        if (ObjectUtil.isNotEmpty(proprietor)) {
            HouseCommission houseCommission = houseCommissionService.selectHouseCommissionByProprietorId(proprietor.getProprietorId());
            ProxyService proxyService = proxyServiceService.selectProxyServiceByProprietorId(proprietor.getProprietorId());
            proprietor.setHouseCommission(houseCommission);
            proprietor.setProxyService(proxyService);
        }
        return proprietor;
    }

    @Override
    public Proprietor selectProprietorByConUserId(Long userId) {
        return proprietorMapper.selectProprietorByUserId(userId);
    }
}
